<?php
require '../../framework/bootstrap.inc.php';
global $_GPC;
$modulelist = pdo_fetchall('select id,settings,uniacid from ' . tablename('uni_account_modules') . 'where module =:module and enabled = :enabled', array(':module' => 'goouc_class', ':enabled' => 1));
foreach ($modulelist as $k => $v) {
    $config = unserialize($v['settings']);
    $set = pdo_get('account_wxapp',array('uniacid'=>$v['uniacid']),array('key','secret'));
    if (isset($config['refund_status']) && $config['refund_status'] == 1) {
        $faile_grouplist = pdo_fetchall("select * from " . tablename('goouc_class_user_group') . " where refund_status = 2 AND weid = :weid AND ( (createtime < :endtime and groupnum < :groupnum) || status <> :status )", [':weid'=>$v['uniacid'],':endtime' => time() - ($config['validtime'] * 3600), ':groupnum' => $config['groupnum'],':status'=>1]);
        foreach ($faile_grouplist as $k => $v) {
            $faile_user_list = pdo_fetchall("select * from " . tablename('goouc_class_user_group_info') . " where groupid = :groupid and refund_status = :refund_status", [':groupid' => $v['id'], ':refund_status' => 2]);
            foreach ($faile_user_list as $kk => $vv) {
                $info = pdo_fetch("select ugi.out_trade_no,o.order_status,o.all_money,o.true_money,o.id,o.transaction_sn,ugi.groupid from " . tablename('goouc_class_user_group_info') . " ugi JOIN " . tablename('goouc_class_order') . " o ON ugi.out_trade_no = o.out_trade_no where ugi.id = :id AND ugi.uid = :uid AND ugi.refund_status = :status", [':id' => $vv['id'],':uid'=>$vv['uid'],':status'=>2]);
                if(!empty($info)){
                    $out_trade_no = $info['out_trade_no'];
                    $total_fee = intval($info['true_money'] * 100);
                    $refund_fee = intval($info['true_money'] * 100);
                    $WxPayConfig = new \WxPayConfig();
                    $WxPayConfig->AppId = $set['key'];
                    $WxPayConfig->AppSecret = $set['secret'];
                    $WxPayConfig->Mchid = $config['mchid'];
                    $WxPayConfig->PaySecret = $config['pay_secret'];
                    $WxPayConfig->sslCertPath = IA_ROOT . '/' .$config['wxcert'];
                    $WxPayConfig->sslKeyPath = IA_ROOT . '/' .$config['wxkey'];
                    $input = new \WxPayRefund();
                    $input->SetTransaction_id($info['transaction_sn']);
                    $input->SetOut_refund_no("refund" . date("YmdHis"));
                    $input->SetOut_trade_no($out_trade_no);
                    $input->SetOp_user_id($WxPayConfig->GetMerchantId());
                    $input->SetTotal_fee($total_fee);
                    $input->SetRefund_fee($refund_fee);
                    $result = WxPayApi::refund($WxPayConfig, $input);
                    $result = [
                        'result_code'=>'SUCCESS',
                        'return_code'=>'SUCCESS'
                    ];
                    if ($result['result_code'] == 'SUCCESS' && $result['return_code'] == 'SUCCESS') {
                        pdo_update('goouc_class_order', array('order_status' => 2, 'refundtime' => time()), array('id' => $info['id']));
                        pdo_update('goouc_class_user_group_info', array('refund_status' => 1, 'refund_time' => time()), ['id' => $id]);
                        //标记拼团表，是否执行过退款
                        pdo_update('goouc_class_user_group', array('refund_status' => 1,'status'=>3), ['id' => $info['groupid']]);
                    }
                }
            }
        }
    }
}
